package com.sgb.member.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.member.MemberConfig;
import com.sgb.member.R;
import com.sgb.member.databinding.ActivityMemberOperationBinding;
import com.sgb.member.di.DaggerMemberComponent;
import com.sgb.member.di.MemberModule;
import com.sgb.member.entity.MaterialFilterJson;
import com.sgb.member.entity.MemberGoodsListEntity;
import com.sgb.member.ui.adapter.HomeOtherMemberAdapter;
import com.sgb.member.view_model.MemberOperationModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.refresh.SuperSwipeRefreshLayout;
import com.swgk.core.view.countmenu.CountMenuEntity;
import com.swgk.core.view.entity.MenuEntity;
import com.swgk.core.view.weiget.CountMenuView;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 会员经营
 */
public class MemberOperationsActivity extends AppActivity {

    @Inject
    public MemberOperationModel sModel;
    private ActivityMemberOperationBinding mBinding;
    private HomeOtherMemberAdapter<MemberGoodsListEntity> mAdapter;
    private List<MenuEntity> mMenuData;
    private String[] titles = new String[]{"最新", "热门", "我的"};

    public static void start(Context context) {
        Intent intent = new Intent(context, MemberOperationsActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMemberComponent.builder()
                .appComponent(appComponent)
                .memberModule(new MemberModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_member_operation);
        mBinding.activityBar.titleView.setText("会员经营");
        mBinding.activityBar.ivBack.setOnClickListener(v -> finish());
        initRecyelerView();
        initSwipe();
        loadData();
    }

    private void initRecyelerView() {
        mAdapter = new HomeOtherMemberAdapter<MemberGoodsListEntity>(this, R.layout.item_other_material_goods, null);
        mAdapter.setOnItemClick(new AdapterOnItemClick<MemberGoodsListEntity>() {
            @Override
            public void onItemClick(MemberGoodsListEntity goodsListEntity, int position) {
                // 商砼 == 7
                if ("7".equals(goodsListEntity.getCid1())) {
                    MaterialFilterJson filterJson = new MaterialFilterJson();
                    filterJson.setStrengthId(goodsListEntity.getSort());
                    MemberConfig.getInstance().startConcreteDetail(MemberOperationsActivity.this, goodsListEntity.getId(), filterJson);
                } else {
                    MemberConfig.getInstance().startShopGoodsDetail(MemberOperationsActivity.this, goodsListEntity.getId(), "", goodsListEntity.getSkuid());
                }
            }
        });
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mBinding.recyclerView.setAdapter(mAdapter);
        mBinding.recyclerView.setPullRefreshEnabled(false);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                loadData();
            }
        });
    }

    private void initSwipe() {
        mBinding.appBarLayout.addOnOffsetChangedListener((appBarLayout, i) -> mBinding.swipeLayout.setEnabled(i >= 0));
        mBinding.swipeLayout.setLoadMore(false);
        mBinding.swipeLayout.setPullRefreshEnabled(true);
        mBinding.swipeLayout.setHeaderView(createHeaderView());
        mBinding.swipeLayout.setOnPullRefreshListener(new SuperSwipeRefreshLayout.OnPullRefreshListener() {
            @Override
            public void onRefresh() {
                textView.setText("正在刷新");
                loadData();
            }

            @Override
            public void onPullDistance(int distance) {

            }

            @Override
            public void onPullEnable(boolean enable) {
                textView.setText(enable ? "释放立即刷新" : "下拉刷新");
            }
        });
    }

    private void loadData() {
        if (mMenuData == null || mMenuData.size() == 0) {
            sModel.getOperationIcons();
        } else {
            sModel.getGoodList(false);
        }
    }

    public void refreshMenus(List<CountMenuEntity> data) {
        mMenuData = new ArrayList<>();
        for (CountMenuEntity countMenuEntity:data ){
            MenuEntity menuEntity = new MenuEntity();
            menuEntity.setAppIcon(countMenuEntity.getAppIcon());
            menuEntity.setAppIconUrl(countMenuEntity.getAppIconUrl());
            menuEntity.setId(countMenuEntity.getId());
            menuEntity.setSelected(countMenuEntity.isSelected());
            menuEntity.setName(countMenuEntity.getName());
            mMenuData.add(menuEntity);
        }
        // 分类
        mBinding.countMenuView.initData(getSupportFragmentManager(), mMenuData, new CountMenuView.OnItemClick() {
            @Override
            public void onItemClick(MenuEntity entity, int position, int num) {
                MemberBusinessActivity.start(MemberOperationsActivity.this);
            }
        });
        sModel.getGoodList(false);
    }

    public void showEmpty() {
        closeRecyclerRefreshing();
        mAdapter.clearDatas();
        mBinding.empView.setVisibility(View.VISIBLE);
    }

    public void closeRecyclerRefreshing() {
        mBinding.recyclerView.refreshComplete();
        mBinding.recyclerView.loadMoreComplete();
        mBinding.swipeLayout.setRefreshing(false);
    }

    public void refreshRecycler(List<MemberGoodsListEntity> list, boolean isLoadMore, boolean noMoreData) {
        closeRecyclerRefreshing();
        mBinding.recyclerView.setVisibility(View.VISIBLE);
        mBinding.recyclerView.setLoadingMoreEnabled(noMoreData);
        if (isLoadMore) {
            if (list != null && list.size() > 0) {
                mAdapter.addDatas(list);
            }
        } else {
            if (list != null && list.size() > 0) {
                mBinding.empView.setVisibility(View.GONE);
                mAdapter.setDatas(list);
            } else {
                mAdapter.clearDatas();
                mBinding.empView.setVisibility(View.VISIBLE);
            }
        }
    }


    private TextView textView;

    private View createHeaderView() {
        @SuppressLint("InflateParams")
        View headerView = LayoutInflater.from(mBinding.swipeLayout.getContext()).inflate(R.layout.super_swipe_refresh_head, null);
        ProgressBar progressBar = headerView.findViewById(R.id.pb_view);
        textView = headerView.findViewById(R.id.text_view);
        textView.setText("下拉刷新");
        progressBar.setVisibility(View.GONE);
        return headerView;
    }
}
